﻿namespace HutongGames.PlayMakerEditor
{
    using HutongGames.PlayMaker;
    using System;
    using UnityEditor;
    using UnityEngine;

    internal static class CircuitLink
    {
        private const float MaxTangentLength = 80f;
        private const float MinTangentLength = 20f;
        private const float TangentYFactor = 0.4f;

        public static void Draw(FsmState fromState, FsmState toState, int transitionIndex, Color linkColor, float linkWidth, Texture leftArrow, Texture rightArrow)
        {
            FsmTransition transition = fromState.Transitions[transitionIndex];
            FsmTransition.CustomLinkConstraint linkConstraint = transition.LinkConstraint;
            float y = (fromState.Position.y + ((transitionIndex + 1) * 16f)) + 8f;
            float num2 = toState.Position.y + 8f;
            float num3 = fromState.Position.x - 1f;
            float num4 = (num3 + fromState.Position.width) + 2f;
            float num5 = toState.Position.x - 1f;
            float num6 = (num5 + toState.Position.width) + 2f;
            Vector3 fromPos = new Vector3(0f, y, 0f);
            Vector3 toPos = new Vector3(0f, num2, 0f);
            Vector3 vector3 = fromPos;
            Vector3 vector4 = toPos;
            float num7 = Mathf.Abs((float) (y - num2)) * 0.4f;
            num7 = Mathf.Clamp(num7, 20f, 80f);
            bool flag = false;
            switch (linkConstraint)
            {
                case FsmTransition.CustomLinkConstraint.None:
                {
                    if ((num6 + 20f) >= (num3 - 20f))
                    {
                        if (num5 < num3)
                        {
                            fromPos.x = num3;
                            toPos.x = num5;
                            float a = fromPos.x - num7;
                            float b = toPos.x - num7;
                            vector3.x = Mathf.Min(a, b);
                            vector4.x = vector3.x;
                        }
                        else if ((num5 - 20f) > (num4 + 20f))
                        {
                            fromPos.x = num4;
                            toPos.x = num5;
                            float num12 = fromPos.x + num7;
                            float num13 = toPos.x - num7;
                            vector3.x = num12 + ((num13 - num12) * 0.3f);
                            vector4.x = vector3.x;
                        }
                        else
                        {
                            fromPos.x = num4;
                            toPos.x = num6;
                            float num14 = fromPos.x + num7;
                            float num15 = toPos.x + num7;
                            vector3.x = Mathf.Max(num14, num15);
                            vector4.x = vector3.x;
                            flag = true;
                        }
                        break;
                    }
                    fromPos.x = num3;
                    toPos.x = num6;
                    float num8 = fromPos.x - num7;
                    float num9 = toPos.x + num7;
                    vector3.x = num8 + ((num9 - num8) * 0.3f);
                    vector4.x = vector3.x;
                    flag = true;
                    break;
                }
                case FsmTransition.CustomLinkConstraint.LockLeft:
                {
                    if (num6 >= num3)
                    {
                        fromPos.x = num3;
                        toPos.x = num5;
                        float num18 = fromPos.x - num7;
                        float num19 = toPos.x - num7;
                        vector3.x = Mathf.Min(num18, num19);
                        vector4.x = vector3.x;
                        break;
                    }
                    fromPos.x = num3;
                    toPos.x = num6;
                    float num16 = fromPos.x - num7;
                    float num17 = toPos.x + num7;
                    vector3.x = num16 + ((num17 - num16) * 0.3f);
                    vector4.x = vector3.x;
                    flag = true;
                    break;
                }
                default:
                    if (num5 > num4)
                    {
                        fromPos.x = num4;
                        toPos.x = num5;
                        float num20 = fromPos.x + num7;
                        float num21 = toPos.x - num7;
                        vector3.x = num20 + ((num21 - num20) * 0.3f);
                        vector4.x = vector3.x;
                    }
                    else
                    {
                        fromPos.x = num4;
                        toPos.x = num6;
                        float num22 = fromPos.x + num7;
                        float num23 = toPos.x + num7;
                        vector3.x = Mathf.Max(num22, num23);
                        vector4.x = vector3.x;
                        flag = true;
                    }
                    break;
            }
            DrawLine(fromPos, vector3, linkColor, linkWidth);
            DrawLine(vector3, vector4, linkColor, linkWidth);
            DrawLine(vector4, toPos, linkColor, linkWidth);
            if (!flag)
            {
                leftArrow = rightArrow;
                toPos.x -= leftArrow.width;
            }
            FsmGraphView.DrawArrowHead(leftArrow, toPos, linkColor, false);
        }

        private static void DrawLine(Vector3 fromPos, Vector3 toPos, Color color, float width)
        {
            Handles.DrawBezier(fromPos, toPos, fromPos, toPos, color, FsmEditorStyles.LineTexture, width * 2f);
        }
    }
}

